package com.ss.android.article.base.feature.detail2.d;

import android.content.Context;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.detail.model.ArticleDetail;
import com.ss.android.article.base.feature.detail.model.ArticleInfo;
import com.ss.android.article.base.feature.detail.presenter.CommentFetcher;
import com.ss.android.article.base.feature.detail.presenter.DetailLoader;
import com.ss.android.article.base.feature.detail2.article.a.NewDetailViewHolder;
import com.ss.android.article.base.feature.detail2.article.b.DetailWebInteractor;
import com.ss.android.article.base.feature.detail2.article.c.NewDetailView;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.model.AbsArticle;
import com.ss.android.newmedia.MediaAppUtil;
import com.ss.android.newmedia.f.Response;
import com.ss.android.newmedia.util.InfoLRUCache;

public class DetailLoadResultHandler implements DetailLoader.Callback {
    private static final String TAG = DetailLoadResultHandler.class.getSimpleName();
    public DetailLoader mDetailLoader;
    public ResultListener<Article, ArticleDetail> mArticleDetailWithCacheResultListener;
    public ResultListener<CommentFetcher, Boolean> mCommentResultListener;
    public DetailWebInteractorHolder mDetailWebInteractorHolder;
    private Context mContext;
    private NewDetailContext mNewDetailContext;
    private InfoLRUCache<Long, ArticleInfo> mItemIdArticleInfoLRUCache;
    private ResultListener<Article,ArticleDetail> mArticleDetailFromInternetResultListener;
    private ResultListener<Article,ArticleInfo> mArticleInfoResultListener;

    public DetailLoadResultHandler(Context context, NewDetailContext newDetailContext) {
        this.mContext = context;
        this.mNewDetailContext = newDetailContext;
        this.mDetailLoader = new DetailLoader(this.mNewDetailContext.categoryName, "", this.mNewDetailContext.adId, this, 0);
        this.mItemIdArticleInfoLRUCache = new InfoLRUCache(8, 8);
    }

    public final ArticleInfo getArticleInfo(long itemId) {
        return this.mItemIdArticleInfoLRUCache.get(Long.valueOf(itemId));
    }

    public final void loadArticleInfo(String itemKey, Article article, String fromWhere, ResultListener<Article,ArticleInfo> resultListener) {
        this.mArticleInfoResultListener = resultListener;
        this.mDetailLoader.loadArticleInfo(itemKey, article, fromWhere);
    }

    public final void loadArticleDetail(String arg2, Article article, AbsArticle absArticle,
                                        boolean isNetworkAvailable,
                                        ResultListener<Article,ArticleDetail> resultListener) {
        this.mArticleDetailFromInternetResultListener = resultListener;
        if (isNetworkAvailable) {
            this.mDetailLoader.loadArticleDetailByLocal(arg2, article, absArticle);
        } else {
            this.mDetailLoader.loadArticleDetail(arg2, article, absArticle);
        }
    }

    public final void onCommentLoaded(CommentFetcher commentFetcher, boolean arg4) {
        if (this.mCommentResultListener != null) {
            this.mCommentResultListener.onResult(commentFetcher, Boolean.valueOf(arg4));
        }
    }

    public final void onArticleInfoLoaded(Article article, ArticleInfo articleInfo) {
        if (article != null && article == this.mNewDetailContext.article) {
            if (articleInfo == null) {
                long itemId = article.groupId;
                if (Logger.debug()) {
                    UIUtils.showImageToast(this.mContext, R.drawable.close_popup_textpage, "load info failed: " + itemId);
                }
            } else {
                if (Logger.debug()) {
                    Logger.d(DetailLoadResultHandler.TAG, "onArticleInfoLoaded " + articleInfo.itemId);
                }

                this.mItemIdArticleInfoLRUCache.put(Long.valueOf(articleInfo.itemId), articleInfo);
                int hasDataUpdate;
                if (articleInfo.diggCount < 0 || article.diggCount == articleInfo.diggCount) {
                    articleInfo.diggCount = -1;
                    hasDataUpdate = 0;
                } else {
                    article.diggCount = articleInfo.diggCount;
                    hasDataUpdate = 1;
                }

                if (articleInfo.buryCount < 0 || article.buryCount == articleInfo.buryCount) {
                    articleInfo.buryCount = -1;
                } else {
                    article.buryCount = articleInfo.buryCount;
                    hasDataUpdate = 1;
                }

                if (articleInfo.videoWatchCount < 0 || article.videoWatchCount >= articleInfo.videoWatchCount) {
                    articleInfo.videoWatchCount = -1;
                } else {
                    article.videoWatchCount = articleInfo.videoWatchCount;
                    hasDataUpdate = 1;
                }

                if (articleInfo.userDigg) {
                    if ((article.userDigg) || (article.userBury)) {
                        articleInfo.userDigg = false;
                    } else {
                        article.userDigg = true;
                        hasDataUpdate = 1;
                    }

                    articleInfo.userBury = false;
                } else {
                    if (!articleInfo.userBury) {
                    } else {

                        if (!article.userDigg && !article.userBury) {
                            article.userBury = true;
                            hasDataUpdate = 1;
                        } else {
                            articleInfo.userBury = false;
                        }
                    }
                }

                String v7 = null;
                if ((android.support.design.a.isTextEmpty(articleInfo.shareUrl)) || (articleInfo.shareUrl.equals(article.shareUrl))) {
                    articleInfo.shareUrl = v7;
                } else {
                    article.shareUrl = articleInfo.shareUrl;
                    hasDataUpdate = 1;
                }

                if ((android.support.design.a.isTextEmpty(articleInfo.displayUrl)) || (articleInfo.displayUrl.equals(article.displayUrl))) {
                    articleInfo.displayUrl = v7;
                } else {
                    article.displayUrl = articleInfo.displayUrl;
                    hasDataUpdate = 1;
                }

                if ((android.support.design.a.isTextEmpty(articleInfo.displayTitle)) || (articleInfo.displayTitle.equals(article.displayTitle))) {
                    articleInfo.displayTitle = v7;
                } else {
                    article.displayTitle = articleInfo.displayTitle;
                    hasDataUpdate = 1;
                }

                if (articleInfo.delete) {
                    article.delete = true;
                    article.commentCount = 0;
                }

                if ((articleInfo.banComment) && !article.mBanComment) {
                    article.mBanComment = true;
                    hasDataUpdate = 1;
                }

                if (hasDataUpdate != 0 && !articleInfo.delete) {
                    DBHelper.getInstance(this.mContext).updateArticleInfo(articleInfo);
                }

                if (this.mArticleInfoResultListener != null) {
                    this.mArticleInfoResultListener.onResult(article, articleInfo);
                }
            }
        }
    }

    public final void onArticleDetailLoadedWithCache(Article article, ArticleDetail articleDetail) {
        if (this.mArticleDetailWithCacheResultListener != null) {
            this.mArticleDetailWithCacheResultListener.onResult(article, articleDetail);
        }
    }

    public final void onLoaded(Article article, AbsArticle absArticle, ArticleDetail articleDetail) {
        String v1;
        String v0 = null;
        if (articleDetail != null) {
            this.mNewDetailContext.articleDetail = articleDetail;
            v1 = articleDetail.content;
            if (!articleDetail.delete) {
                if (article == null && articleDetail.article != null && articleDetail.article.articleType == 1) {
                    v0 = articleDetail.article.articleUrl;
                }

                if (this.mNewDetailContext.article != null) {
                } else if (articleDetail.article == null) {
                } else {
                    this.mNewDetailContext.article = articleDetail.article;
                    this.mNewDetailContext.groupFlags = articleDetail.article.groupFlags;
                    this.mNewDetailContext.articleType = articleDetail.article.articleType;
                }
            } else if (this.mArticleDetailFromInternetResultListener != null) {
                this.mArticleDetailFromInternetResultListener.onResult(article, articleDetail);
                return;
            }
        } else {
            v1 = v0;
        }
        int v0_1 = !android.support.design.a.isTextEmpty(v1) || !android.support.design.a.isTextEmpty(v0) ? 0 : 1;
        if (!this.mNewDetailContext.isNativePicGroup() && v0_1 != 0 && (NetworkUtils.isNetworkAvailable(this.mContext))) {
            this.mDetailLoader.loadArticleDetail(absArticle.getItemKey(), article, absArticle);
            return;
        }

        if ((this.mNewDetailContext.isNativePicGroup()) && (articleDetail == null || !articleDetail.hasGalleryImages())) {
            if (NetworkUtils.isNetworkAvailable(this.mContext)) {
                this.mDetailLoader.loadArticleDetail(absArticle.getItemKey(), article, absArticle);
                return;
            }

            if (this.mArticleDetailFromInternetResultListener != null) {
                this.mArticleDetailFromInternetResultListener.onResult(this.mNewDetailContext.article, articleDetail);
                return;
            } else {
                return;
            }
        }

        if (this.mArticleDetailFromInternetResultListener != null) {
            this.mArticleDetailFromInternetResultListener.onResult(this.mNewDetailContext.article, articleDetail);
        }
    }

    public final void onLoaded(String arg11, long arg12, Response response) {
        boolean v2_1;
        String v1 = null;
        if (this.mDetailWebInteractorHolder != null) {
            DetailWebInteractorHolder v4 = this.mDetailWebInteractorHolder;
            Long v0 = Long.valueOf(arg12);
            if (!android.support.design.a.isTextEmpty(arg11) && response != null) {
                NewDetailViewHolder v5 = v4.detailWebInteractor.mNewDetailViewHolder;
                if (v5 != null) {
                    Article v2 = v4.detailWebInteractor.mNewDetailContext.article;
                    if (v2 != null && v2.groupId == v0.longValue() && (DetailWebInteractor.hasView(v4.detailWebInteractor))) {
                        v4.detailWebInteractor.mResponse = response;
                        String v0_1 = response.responseCode != 200
                                || response.requestUrl == null
                                || (MediaAppUtil.a(arg11, response.requestUrl))
                                || !MediaAppUtil.a(arg11, v2.articleUrl) ? v1 : response.requestUrl;
                        if (v4.detailWebInteractor.j == 2) {
                            if (v2.hasArticleUrl()) {
                                v1 = v2.articleUrl;
                                v2_1 = v2.d();
                                if (v0_1 != null) {
                                    v1 = v0_1;
                                }
                            } else {
                                v2_1 = false;
                            }

                            if (!android.support.design.a.isTextEmpty(v1)) {
                                if (v4.detailWebInteractor.mNewDetailContext.adId <= 0) {
                                    v4.detailWebInteractor.mMyWebViewClient.a(v1);
                                }

                                if (!DetailWebInteractor.hasView(v4.detailWebInteractor)) {
                                } else {
                                    ((NewDetailView) DetailWebInteractor.getView(v4.detailWebInteractor)).a(v5, v1, v2_1);
                                }
                            }

                            v4.detailWebInteractor.j = 0;
                            return;
                        }

                        v4.detailWebInteractor.j = 0;
                    }
                }
            }
        }
    }

    public final void onArticleDetailLoadedFromInternet(String arg3, Article article, ArticleDetail articleDetail) {
        if (articleDetail != null) {
            this.mNewDetailContext.articleDetail = articleDetail;
            if (this.mNewDetailContext.article == null && articleDetail.article != null) {
                this.mNewDetailContext.article = articleDetail.article;
                this.mNewDetailContext.groupFlags = articleDetail.article.groupFlags;
                this.mNewDetailContext.articleType = articleDetail.article.articleType;
            }
        }

        if (this.mArticleDetailFromInternetResultListener != null) {
            this.mArticleDetailFromInternetResultListener.onResult(article, articleDetail);
        }
    }

    public interface ResultListener<T1, T2> {
        void onResult(T1 arg1, T2 arg2);
    }

    public static final class DetailWebInteractorHolder {
        private final DetailWebInteractor detailWebInteractor;

        public DetailWebInteractorHolder(DetailWebInteractor detailWebInteractor) {
            this.detailWebInteractor = detailWebInteractor;
        }
    }
}

